package com.zhkj.txg.module.login.ui

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelFragment
import com.zhkj.txg.R
import com.zhkj.txg.module.login.vm.LoginViewModel
import com.zhkj.txg.utils.TextWatcherWrapper
import kotlinx.android.synthetic.main.fragment_captcha.*
import java.util.*

class CaptchaFragment : ViewModelFragment<LoginViewModel>() {

    private val requestAgreement = 10001
    override fun setViewModel(): LoginViewModel = createViewModel(true)

    override val layoutResId: Int = R.layout.fragment_captcha

    override fun initUI() {
        // 显示发送验证码的提示文案
        val formatMobile = hideMiddleMobile(viewModel.mobile2GetCode)
        tvSendMobile.text = String.format(getString(R.string.send_to_user), formatMobile)
        btnConfirm.text =
            if (viewModel.mobileIsNewUser) getString(R.string.register) else getString(R.string.login)
        // 协议按钮点击事件
        tvAgreement.setOnClickListener {
            UserAgreementActivity.startForResult(this, requestAgreement)
        }
        // 根据验证码长度判断登录按钮是否可点击
        etCaptcha.addTextChangedListener(TextWatcherWrapper {
            btnConfirm.isEnabled = !TextUtils.isEmpty(it) && it!!.length >= 4
        })
        // 登录按钮点击事件
        btnConfirm.setOnClickListener {
            if (!cbLogin.isChecked) {
                toast(getString(R.string.please_agree_agreement))
                return@setOnClickListener
            }
            viewModel.login(etCaptcha.text.toString()).takeIf {
                !it.hasObservers()
            }?.observe(viewLifecycleOwner, Observer {
                handlerResponseStatus(it.status, it.statusTip)
            })
        }
        // 发送验证码
        tvSendCode.setOnClickListener {
            viewModel.sendCode(viewModel.mobile2GetCode).takeIf {
                !it.hasObservers()
            }?.observe(viewLifecycleOwner, Observer {
                handlerResponseStatus(it.status, it.statusTip)
            })
        }
    }

    override fun initViewModel() {
        // 倒计时LiveData观察
        viewModel.countDownLiveData.observe(viewLifecycleOwner, Observer {
            if (it > 0) {
                tvSendCode.text =
                    String.format(Locale.getDefault(), getString(R.string.second_resend), it)
                tvSendCode.isEnabled = false
            } else {
                tvSendCode.text = getString(R.string.resend)
                tvSendCode.isEnabled = true
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == requestAgreement && resultCode == Activity.RESULT_OK) {
            val agree = data?.getBooleanExtra(AGREE, false) ?: false
            if (agree) cbLogin.isChecked = true
        }
    }

    private fun hideMiddleMobile(mobile: String?): String {
        return if (!TextUtils.isEmpty(mobile) && mobile!!.length >= 11) {
            mobile.replaceRange(3, 7, "****")
        } else ""
    }
}